<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组验证示例</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7fc;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            flex-direction: column;
        }

        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 20px;
        }

        .container {
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 80%;
            max-width: 500px;
            text-align: center;
        }

        input,
        select,
        button {
            padding: 10px;
            margin-bottom: 15px;
            width: 80%;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            background-color: #4CAF50;
            color: white;
            cursor: pointer;
        }

        button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            padding: 10px;
            background-color: #f9f9f9;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        .result p {
            margin: 5px 0;
            font-weight: bold;
        }

        .error {
            color: red;
        }
    </style>
</head>

<body>

    <h1>数组验证示例</h1>
    <div class="container">
        <h3>请输入数组和选择验证条件</h3>

        <!-- 用户输入数组 -->
        <input type="text" id="arrayInput" placeholder="请输入数组 (例如: [1, 2, 3])">

        <!-- 选择验证条件 -->
        <select id="conditionSelect">
            <option value="Boolean">检查所有值是否为真</option>
            <option value="isPositive">检查所有值是否大于0</option>
        </select>

        <!-- 按钮触发验证 -->
        <button onclick="validateArray()">验证数组</button>

        <!-- 结果展示 -->
        <div class="result" id="result"></div>
    </div>

    <script>
        // all函数，用于验证数组的所有元素是否符合给定的条件
        const all = (arr, fn = Boolean) => arr.every(fn);

        // 根据用户输入的条件验证数组
        function validateArray() {
            const input = document.getElementById('arrayInput').value.trim();
            const condition = document.getElementById('conditionSelect').value;
            const resultDiv = document.getElementById('result');
            resultDiv.innerHTML = ''; // 清空之前的结果

            // 解析输入的数组
            let arr;
            try {
                arr = JSON.parse(input);
                if (!Array.isArray(arr)) {
                    throw new Error('不是有效的数组');
                }
            } catch (e) {
                resultDiv.innerHTML = '<p class="error">请输入一个有效的数组格式！</p>';
                return;
            }

            // 根据条件选择验证函数
            let fn;
            if (condition === 'Boolean') {
                fn = Boolean; // 默认的布尔值检查
            } else if (condition === 'isPositive') {
                fn = value => value > 0; // 检查值是否大于0
            }

            // 验证数组
            const result = all(arr, fn);
            resultDiv.innerHTML = result
                ? '<p>所有条件验证通过！</p>'
                : '<p class="error">条件验证未通过！</p>';
        }
    </script>

</body>

</html>